package adapter;

import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.lidroid.xutils.BitmapUtils;

import org.mobiletrain.www.secondproject2.R;

import java.util.ArrayList;

import Utils.ImageUtils;
import bean.TopPagerData;


public class TopPagerAdapter extends PagerAdapter{
    private ArrayList<TopPagerData> datas;
    private Context context;
    private ArrayList<View> views;

    public TopPagerAdapter(ArrayList<TopPagerData> datas, Context context) {
        this.datas = datas;
        this.context = context;
        init();

    }
    public void init(){
        views = new ArrayList<View>();
        views.clear();
        if (datas.size()<5){
            return;
        }
        for (int i = 0; i < 5; i++) {
            View view = LayoutInflater.from(context).inflate(R.layout.topview_layout, null);
            ImageView topIcon = (ImageView) view.findViewById(R.id.top_icons);

            TopPagerData topPagerData = datas.get(i);
            topIcon.setImageResource(R.drawable.ic_launcher);
            BitmapUtils utils = ImageUtils.getBitmapUtils(context);
            utils.display(topIcon,topPagerData.getApp_picpath());

            views.add(view);
        }
    }

    @Override
    public void notifyDataSetChanged() {
        init();
        super.notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return views.size();
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = views.get(position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
       // super.destroyItem(container, position, object);
        container.removeView(views.get(position));
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }
}
